<?php
namespace app\manage\controller;
use app\common\controller\Manage;
use app\common\model\Shop as ShopModel;
use app\common\model\ShopSort;
use app\common\model\User;
use think\Db;
class Shop extends Manage
{

	public function list()
	{
		if(request()->isPost()){
			$data=input('post.');
            $this->searchvar($data);
            $list=(new ShopModel)->withSearch(
                ['ctime','name','status'],
                ['ctime'=>[$this->start,$this->end],'name'=>$this->search,'status'=>$this->status])
            ->withJoin(['user'=>['nickname'],'shopsort'=>['name']])
            ->order('id asc')
            ->paginate(['list_rows'=>$this->limit,'page'=>$this->page])->toArray();
            return json(['code'=>0,'data'=>$list['data'],'count'=>$list['total']]);
		}else{
			return view();
		}
	}
	/**
	 * 店铺审核
	 * @return [type] [description]
	 */
	public function checkshop()
	{
		if(request()->isPost()){
            Db::startTrans();
            $data=input('post.');
            $info=(new ShopModel)->where('id',$data['id'])->where('status',0)->find();
            if(!$info){
                Db::rollback();
                return json(['code'=>0,'msg'=>'数据错误']);
            }
            $info->status=$data['status'];
            $info->utime=time();
            if(!$info->save()){
                Db::rollback();
                return json(['code'=>0,'msg'=>'审核失败']);
            }
            if($data['status']==1){
                $user=(new User)->where('id',$info->uid)->where('is_shop',0)->find();
                if(!$user){
                    Db::rollback();
                    return json(['code'=>0,'msg'=>'用户信息错误']);
                }
                $user->is_shop=1;
                if(!$user->save()){
                    Db::rollback();
                    return json(['code'=>0,'msg'=>'审核失败']);
                }
            }
            Db::commit();
            return json(['code'=>1,'msg'=>'成功']);
        }
	}
	/**
	 * 类目列表
	 * @return [type] [description]
	 */
	public function sortlist()
	{
		if(request()->isPost()){
			$data=input('post.');
            $this->searchvar($data);
            $list=(new ShopSort)->withSearch(
                ['ctime','name','status'],
                ['ctime'=>[$this->start,$this->end],'name'=>$this->search,'status'=>$this->status])
            ->order('id asc')
            ->select();
            return json(['code'=>0,'data'=>category($list),'count'=>0]);
		}else{
			return view();
		}
	}
	/**
	 * 保存类目
	 * @return [type] [description]
	 */
	public function addsort()
	{
		$id=input('get.id');
		if(request()->isPost()){
			$data=input('post.');
			if(!isset($data['status'])){
				$data['status']=0;
			}
			if($id){
				$i=(new ShopSort)->where('id',$id)->update($data);
			}else{
				$i=(new ShopSort)->save($data);
			}			
			if(!$i){
				return json(['code'=>0,'msg'=>'保存失败']);
			}
			return json(['code'=>1,'msg'=>'保存成功']);
		}else{
			$list=(new ShopSort)->where('pid',0)->where('status',1)->select();
			$assign['list']=$list;
			if($id){
				$info=(new ShopSort)->where('id',$id)->find();
				$assign['info']=$info;
			}
			return view('',$assign);
		}
	}
	/**
	 * 删除类目
	 * @return [type] [description]
	 */
	public function delsort()
	{
		if(request()->isPost()){
			$id=input('post.id');
			$i=(new ShopSort)->where('id',$id)->delete();
			if(!$i){
				return json(['code'=>0,'msg'=>'删除失败']);
			}
			return json(['code'=>1,'msg'=>'删除成功']);
		}
	}
}